package cn.hfuu.service.impl;

import cn.hfuu.dao.UserDao;
import cn.hfuu.domain.User;
import cn.hfuu.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import org.hibernate.bytecode.buildtime.spi.ExecutionException;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User getUserByCodePassword(User user) {
        //根据用户名来查询用户，判断用户是否存在
        User existU = userDao.getUserByUserCode(user.getUser_code());

        //判断用户是否存在
        if(existU!=null) {
            if(!existU.getUser_password().equals(user.getUser_password())){
                throw new RuntimeException("password error");
            }
            else{
                return existU;
            }
        }
        else{
            throw new RuntimeException("inexistent user!!");
        }


    }

    @Override
    public void registerUser(User user) throws Exception{
        //根据用户名查询用户，判断是否用户名重复
            if(userDao.getUserByUserCode(user.getUser_code())!=null) {
                throw new RuntimeException("用户已存在");
            }
            else {
                userDao.save(user);
            }
    }
}
